/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pfc.modelocg;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import org.apache.ddlutils.model.Database;
import org.dom4j.DocumentException;
import pfc.modelocg.CGForm.FormType;
import pfc.modeloer.ERModel;
import pfc.modeloer.EREntity;

/**
 *
 * @author xandru
 */
public class CGModel {

    String _name;
    ERModel _model;
    Database _db;
    ArrayList<CGForm> _forms = new ArrayList<CGForm>();
    ArrayList<CGEntityForms> _entityForms = new ArrayList<CGEntityForms>();

    public CGModel(ERModel m, Database db)
    {
       this._name = m.getName();
       this._model = m;
       this._db = db;

       createFromERModel();
    }

    private CGModel(){};

    public static CGModel readFromFile(String fichero)
    {
        CGModel cg = new CGModel();
        try {
            CGModelIO.XML2Model(cg, fichero);
        } catch (DocumentException ex) {
            Logger.getLogger(CGModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(CGModel.class.getName()).log(Level.SEVERE, null, ex);
        }

        return cg;
    }


    private void createFromERModel() {
        for (EREntity e: _model.getEntityList()){

            CGEntityForms ef = new CGEntityForms(this,e.getName());
            this._entityForms.add(ef);

            _forms.add (new CGForm(e,FormType.EditForm));
            _forms.add (new CGForm(e,FormType.LinesForm));
            _forms.add (new CGForm(e,FormType.SearchForm));




        }
    }

    public Iterable<CGForm> getFormForEntity(EREntity e)
    {
        ArrayList<CGForm> l = new ArrayList<CGForm>();

        for(CGForm f:this.getForms())
        {
            if (StringUtils.equals(f.getEntityName(), e.getName()))
                l.add( f );
        }

        return l;
    }

    public Iterable<CGForm> getForms() {
        return _forms;
    }

    public void addForm(CGForm f)
    {
        this._forms.add(f);
    }

    public Iterable<CGEntityForms> getEntityForms() {
        return this._entityForms;
    }

    void addEntityForms(CGEntityForms efs) {
        this._entityForms.add(efs);
    }


    public CGEntityForms findEntityForms(String entity)
    {
       for (CGEntityForms cg: this._entityForms)
       {
           if (cg.getEntityName().equals(entity))
               return cg;
       }

       return null;
    }

    void removeForm(String entityName, FormType formType) {
        CGForm toRemove=null;
        for (CGForm f: this.getForms())
        {
            if ((f.getEntityName().equals(entityName))
               && (f.getType()==formType))
            {
                toRemove = f;
                break;
            }
        }

        if (toRemove!=null)
            this._forms.remove(toRemove);
    }


}
